<?php

/**
 * Represents a user-defined form.
 * @author Yaron Koren
 * @file
 * @ingroup SF
 */
class SFForm
{
    private $mFormName;
    private $mTemplates;
    private $mPageNameFormula;
    private $mCreateTitle;
    private $mEditTitle;
    private $mAssociatedCategory;

    static function create( $formName, $templates )
    {
        $form = new SFForm();
        $form->mFormName = ucfirst( str_replace( '_', ' ', $formName ) );
        $form->mTemplates = $templates;
        $form->mAssociatedCategory = null;
        return $form;
    }

    function getFormName()
    {
        return $this->mFormName;
    }

    function setPageNameFormula( $pageNameFormula )
    {
        $this->mPageNameFormula = $pageNameFormula;
    }

    function setCreateTitle( $createTitle )
    {
        $this->mCreateTitle = $createTitle;
    }

    function setEditTitle( $editTitle )
    {
        $this->mEditTitle = $editTitle;
    }

    function setAssociatedCategory( $associatedCategory )
    {
        $this->mAssociatedCategory = $associatedCategory;
    }

    function creationHTML()
    {
        $text = "";
        foreach ( $this->mTemplates as $i => $ft ) {
            $text .= $ft->creationHTML( $i );
        }
        return $text;
    }

    function createMarkup( $inputs = null, $freeTextLabel = null )
    {

        $defaultInputs = array(
            'free text' => '{{{standard input|free text|rows=10}}}',
            'summary' => '{{{standard input|summary}}}',
            'minor edit' => '{{{standard input|minor edit}}}',
            'watch' => '{{{standard input|watch}}}',
            'save' => '{{{standard input|save}}}',
            'preview' => '{{{standard input|preview}}}',
            'changes' => '{{{standard input|changes}}}',
            'cancel' => '{{{standard input|cancel}}}',
        );

        if ( $inputs === null || !count( $inputs ) ) {
            $inputs = $defaultInputs;
        }

        $title = Title::makeTitle( SF_NS_FORM, $this->mFormName );
        $fs = SFUtils::getSpecialPage( 'FormStart' );
        $form_start_url = SFUtils::titleURLString( $fs->getTitle() ) . "/" . $title->getPartialURL();
        $form_description = wfMessage( 'sf_form_docu', $this->mFormName, $form_start_url )->inContentLanguage()->text();
        $form_input = "{{#forminput:form=" . $this->mFormName;
        if ( !is_null( $this->mAssociatedCategory ) ) {
            $form_input .= "|autocomplete on category=" . $this->mAssociatedCategory;
        }
        $form_input .= "}}\n";
        $text = <<<END
<noinclude>
$form_description


$form_input
</noinclude><includeonly>

END;
        if ( !empty($this->mPageNameFormula) || !empty($this->mCreateTitle) || !empty($this->mEditTitle) ) {
            $text .= "{{{info";
            if ( !empty($this->mPageNameFormula) ) {
                $text .= "|page name=" . $this->mPageNameFormula;
            }
            if ( !empty($this->mCreateTitle) ) {
                $text .= "|create title=" . $this->mCreateTitle;
            }
            if ( !empty($this->mEditTitle) ) {
                $text .= "|edit title=" . $this->mEditTitle;
            }
            $text .= "}}}\n";
        }
        $text .= <<<END
<div id="wikiPreview" style="display: none; padding-bottom: 25px; margin-bottom: 25px; border-bottom: 1px solid #AAAAAA;"></div>

END;
        foreach ( $this->mTemplates as $template ) {
            $text .= $template->createMarkup() . "\n";
        }
        if ( $freeTextLabel === null || empty($freeTextLabel) ) {
            $free_text_label = wfMessage( 'sf_form_freetextlabel' )->inContentLanguage()->text();
        } else {
            $free_text_label = $freeTextLabel;
        }

        foreach ( $inputs as $inputName => $inputText ) {

            if ( (empty($inputText) || $inputText == '1') && array_key_exists( $inputName, $defaultInputs ) ) {
                $inputText = $defaultInputs[$inputName];
            }

            if ( $inputName == 'free text' ) {
                $text .= $free_text_label . "\n";
                $text .= $inputText . "\n\n";
                continue;
            }
            if ( $inputName == 'summary' || $inputName == 'minor edit' || $inputName == 'watch' ) {
                $text .= $inputText . "\n\n";
                continue;
            }
            $text .= $inputText . "\t";

        }

        return $text;
    }

}
